package com.xmb.common.controller.internal;

import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.xmb.common.exception.BusinessException;
import com.xmb.common.params.WxPayUnifiedOrdeParam;
import com.xmb.common.service.wx.WxPayRefundService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by lyndon on 2019/4/24.
 */
@RestController
@RequestMapping(value = "/refund")
public class WxRefundController {

    @Autowired
    private WxPayRefundService payRefundService;

    /**
     * 自动退款接口
     * @param
     */
    @PostMapping(value = "autoRefundOrder")
    public String refundOrder(@RequestBody WxPayUnifiedOrdeParam ordeParam) throws WxPayException {
        if (ordeParam.getOrderId() <=0) {
            throw new BusinessException("业务订单号不合法");
        }
        if (StringUtils.isBlank(ordeParam.getOrderType())) {
            throw new BusinessException("订单类型不能为空");
        }
        WxPayRefundResult wxPayRefundResult = payRefundService.autoRefundOrder(ordeParam);
        return wxPayRefundResult.getOutRefundNo();
    }
}
